package edu.ubb.scrumster.model.repository;

import java.util.ArrayList;
import java.util.List;

import edu.ubb.scrumster.model.model.AbstractModel;
import edu.ubb.scrumster.model.model.Sprint;
import edu.ubb.scrumster.model.model.UserStory;

public class UserStoryRepository {

	public UserStoryRepository() {
	}

	public ArrayList<UserStory> getAllUserStory() {
		ArrayList<UserStory> list = new ArrayList<UserStory>();
		for (AbstractModel d : DatabaseAccess.getInstance().getAll(DatabaseAccess.USERSTORY)){
			list.add(getUserStoryById(((UserStory) d).getId()));
		}
		return list;
	}

	public void addUserStory(UserStory g) {
		DatabaseAccess.getInstance().addData(g);
	}

	public void updateUserStory(UserStory data) {
		DatabaseAccess.getInstance().updateData(data);
	}

	public void deleteUserStory(UserStory data) {
		DatabaseAccess.getInstance().deleteData(data);
	}

	public UserStory getUserStoryById(int id) {
		UserStory u  = (UserStory)DatabaseAccess.getInstance().getDataById(id, DatabaseAccess.USERSTORY);
		u.setSprint((Sprint)DatabaseAccess.getInstance().getDataById(u.getSprint().getId(), DatabaseAccess.SPRINT));
		return u;
	}

	public List<UserStory> getUserStoryBySprint(int id) {
		ArrayList<UserStory> list = new ArrayList<UserStory>();
		for (AbstractModel d : DatabaseAccess.getInstance().getUserStoryBySprint(id)){
			list.add(getUserStoryById(((UserStory) d).getId()));
		}
		return list;
	}
}
